package com.seven.commons;

import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments;
import lombok.Data;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * @projectname:common-utils
 * @author: sevenDay
 * @date: 2019/7/29 16:49
 * @desc:
 */
public class Reflect {
    public static void main(String[] args) throws Exception {
        //通过反射获取到方法
        /*Method declaredMethod = Reflect.class.getDeclaredMethod("findStr", int.class, Map.class);
        //获取到方法的参数列表
        Type[] parameterTypes = declaredMethod.getGenericParameterTypes();
        for (Type type : parameterTypes) {
            System.out.println(type);
            //只有带泛型的参数才是这种Type，所以得判断一下
           if(type instanceof ParameterizedType){
                ParameterizedType parameterizedType = (ParameterizedType) type;
                //获取参数的类型
                System.out.println(parameterizedType.getRawType());
                //获取参数的泛型列表
                Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
                for (Type type2 : actualTypeArguments) {
                    System.out.println(type2);
                }
            }
        }*/
        Method declaredMethod = Reflect.class.getDeclaredMethod("findStr", int.class,Map.class);
        //获取返回值的类型，此处不是数组，请注意智商，返回值只能是一个
        Class genericReturnType = declaredMethod.getReturnType();
        System.out.println(declaredMethod.getGenericReturnType().toString());
        System.out.println(genericReturnType);

        String json = "[{\n" +
                "\"name\":\"zhangfuhong\",\n" +
                "\"no\":2019929\n" +
                "},\n" +
                "{\n" +
                "\"name\":\"zhangfuhong1\",\n" +
                "\"no\":20199211\n" +
                "}]\n";
        Object o = JsonUtils.jsonToPojo(json, genericReturnType);
        System.out.println(o);
        System.out.println(JsonUtils.objectToJson(o));
        //获取返回值的泛型参数
       /* if(genericReturnType instanceof ParameterizedType){
            Type[] actualTypeArguments = ((ParameterizedType)genericReturnType).getActualTypeArguments();
            for (Type type : actualTypeArguments) {
                System.out.println(type);
            }
        }*/
    }

    public static List<?> findStr(int id, Map<Integer, String> map){
        return null;
    }


    @Data
    class Student{

        private String name;

        private Integer no;
    }
}
